<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="never">
          <el-form ref="queryFormRef" :model="collectDTO" :inline="true">
            <el-form-item label="年-月" prop="contrastDate">
              <el-date-picker
                v-model="collectDTO.contrastDate"
                type="month"
                value-format="YYYY-MM"
                placeholder="请选择"
                :disabled-date="disabledDate"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-dropdown v-model="collectDTO.type" clearable @command="collect" style="margin-left:15px">
                <el-button type="primary" plain v-hasPermi="['collect:extract:collect']">
                  <el-icon class="el-icon--left">
                    <Coin />
                  </el-icon>
                  采集
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu v-for="dict in data_type" :key="dict.value">
                    <el-dropdown-item :command="dict.value + '-' + dict.label" :icon="dict.value === 'expense_type' ? VideoPlay : Monitor">
                      {{ dict.label }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table border v-loading="loading" :data="extractList" class="enhanced-table">
        <el-table-column label="主键id" align="center" prop="id" v-if="false" />
        <el-table-column label="年-月" align="center" prop="contrastDate" />
        <el-table-column label="采集时间" align="center" prop="createTime" />
        <el-table-column label="类型" align="center" prop="type">
          <template #default="scope">
            <dict-tag :options="data_type" :value="scope.row.type" />
          </template>
        </el-table-column>
        <el-table-column label="支出总额" align="center" prop="expendTotal" />
        <el-table-column label="收入总额" align="center" prop="incomeTotal" />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="data_extract" :value="scope.row.status" />
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="Extract" lang="ts">
import { listExtract, collectData } from '@/api/collect/extract';
import { ExtractVO, ExtractQuery } from '@/api/collect/extract/types';
import { Coin, Monitor, VideoPlay } from '@element-plus/icons-vue';
import useDate from '@/hooks/useDate';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { data_type, data_extract } = toRefs<any>(proxy?.useDict('data_type', 'data_extract'));

const extractList = ref<ExtractVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);

const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const collectDTO = ref({
  contrastDate: useDate.getPreviousMonth(),
  type: undefined
});

const queryParams = ref<ExtractQuery>({
  pageNum: 1,
  pageSize: 10,
  contrastDate:collectDTO.value.contrastDate
});


const disabledDate = (date: Date) => {
  return (
    date.getFullYear() > new Date().getFullYear() || (date.getFullYear() === new Date().getFullYear() && date.getMonth() >= new Date().getMonth())
  );
};
// const calendarChange = (e: any) => {
//   console.log(e);
//   collectDTO.value.contrastDate = e.toString();
// };
/** 查询数据抽取列表 */
const getList = async () => {
  loading.value = true;
  const res = await listExtract(queryParams.value);
  extractList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/**采集数据*/
const collect = (type: string) => {
  if (!collectDTO.value.contrastDate) {
    proxy.$modal.msgWarning('请选择月份');
    return;
  }
  let arr = type.split('-');

  proxy.$modal.confirm('确定要抽取（' + arr[1] + collectDTO.value.contrastDate + '月份）的数据？').then(() => {
    collectDTO.value.type = arr[0];
    collectData(collectDTO.value).then(() => {
      proxy.$modal.msgSuccess('采集成功');
      getList();
    });
  });
};
/** 搜索按钮操作 */
const handleQuery = () => {
  // 将 collectDTO 中的值同步到 queryParams
  queryParams.value.contrastDate = collectDTO.value.contrastDate;
  queryParams.value.pageNum = 1;
  getList();
};

// /** 导出按钮操作 */
// const handleExport = () => {
//   proxy?.download("collect/extract/export", {
//     ...queryParams.value
//   }, `extract_${new Date().getTime()}.xlsx`);
// };

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped></style>
